Thread: Repetition in do{}while

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Your solution only works if they only enter "key""enter". If they enter more than one "key" before hitting enter, it'll loop for as many "key"s you press. Replace your getchar line with the while solution Thantos mentioned.

    Remember, you have to code down to the level of the stupidest user.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    i tried that after you implement that the program won't let you enter anything on the second time around i was just saying that the getchar() there is a quick solution
    Woop?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by prog-bman
    i tried that after you implement that the program won't let you enter anything on the second time around i was just saying that the getchar() there is a quick solution
    I can't understand what it is you're trying to say in that sentence. See how benificial it is when you use capitalization and punctuation?

    At any rate, what's not "quick" about this solution? It takes 30 seconds to type the whole thing, and it works exactly as it should. (Considering EOF usually kills the entire program anyway, there's no need to bother testing for it, especially since the input is buffered anyway.)
    Code:
    #include <stdio.h>
    
    int main( void )
    {
            int c;
            do
            {
                    printf("This is a menu.\n");
                    printf("This is a prompt. > ");
                    c = getchar( );
                    while( getchar( ) != '\n' );
    
            } while ( c != 'Q' && c != 'q' );
    
            return 0;
    }
    Presto. Perfect menuing.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Not perfect menuing after the loop runs once you have to input your answer twice in order for it to get picked up by the switch
    Woop?

  5. #20
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Problems with my latest solution: If there are characters which is a valid case then it will execute automatically.
    Problems with Quzah's solution: Same basic problem.

    Solution: Using better input controls. Expanded: either flush the buffer right after scanf() or use a solution (fgets()) that won't leave characters in the buffer. Otherwise no matter how idiot proof you think you made it someone will come along and find a problem.

  6. #21
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Good call go with fgets totally forgot about that :P
    Woop?

  7. #22
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Thanks a lot guys.

    This discussion is always productive.

    I knew a nested while loop would be able to do this, but I kept getting a compiler error when placing it down. The problem there was because of misplaced brackets.

    The nested do while loop you posted Thantos is an awesome idea, I didn't know that you could just declare stuff to be false either, which should be really helpful.

    I'm going to go read up on fgets, and how to flush the input buffer.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm going to go read up on fgets


    >and how to flush the input buffer
    Yes, do. But before you do, commit to memory the fact that if you need to flush the input buffer, you've already done something wrong. 1) If you need to then your program is ill-designed and you should fix it rather than apply a Band-Aid and 2) The techniques for "flushing the input stream" still aren't a solid solution, if you consider them a solution at all.
    My best code is written with the delete key.

  9. #24
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I didn't know that you could just declare stuff to be false either
    Opps sorry been doing a little too much c++ programming

    No no I just "forgot" to include my enum:
    Code:
     enum {false = 0, true =1};
    Yeah yeah

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by prog-bman
    Not perfect menuing after the loop runs once you have to input your answer twice in order for it to get picked up by the switch
    No it doesn't. Did you even run it? You don't have to enter anything twice.

    There is no problem with my example. Run it and see. I dare you to break this using anything other than EOF to kill it (or some signal or what not). Just using standard input, you cannot break mine, and it does not give invalid entries, and does not make you do it twice.

    The only thing it does "wrong" is that if you enter a valid choice as your first letter, followed by extra crap, it will take the first and kill the rest.
    Code:
    #include <stdio.h>
    int main( void )
    {
        int c;
        do
        {
            printf("Menu:\n");
            printf("1) foo \n");
            printf("2) bar \n");
            printf("Q) quit \n");
            printf("> ");
    
            c = getchar( );
            while( getchar() != '\n' );
            if( c == '1' )
                printf("you chose one\n");
            else if ( c == '2' )
                printf("you chose two\n");
            else if ( c == 'q' || c == 'Q' )
                printf("goodbye\n");
            else
                printf("try again");
        } while( c != 'q' && c != 'Q' );
        return 0;
    }
    The only "problem" is that it doesn't care if you enter a valid followed by invalid data, because it only cares about the first key. But worse case scenario, you have to make your choice because you entered the first key as invalid, which from the description, is what you want the menu to do anyway.

    This does not make you enter anything twice. I don't know what crack you're smoking, but put the pipe down long enough to compile it and acknowledge the fact that you're wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quzah i wasn't talking about your program I was talking about his. Your method yours works pefectly fine for your example
    Last edited by prog-bman; 06-16-2004 at 10:51 PM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  2. repetition with the "for" structure
    By Lillian in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 09:28 PM
  3. Repetition of a line
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2002, 04:44 PM
  4. Controlling Repetition in a Structure
    By Silence in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 05:35 PM
  5. repetition and selection- can anyone help?
    By Tarls in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2002, 10:18 PM